/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import domen.User;
import ejb.UserFacade;
import java.net.UnknownHostException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpSession;
import mail.MailServer;

/**
 *
 * @author Danilo
 */
@ManagedBean
@RequestScoped
public class ForgotManagedBean {

    private String username;
    private String email;
    private String sessionId;
    @EJB
    private UserFacade ufr;
    
    /** Creates a new instance of ForgotManagedBean */
    public ForgotManagedBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        sessionId = session.getId();
    }

    public void resetPass(){
        User u = ufr.find(username);
        if (u != null) {
            if (u.getEmail().equals(email)) {
                try {
                    u.setSessionId(sessionId);
                    u.setActivated(false);
                    new MailServer().sendMail("danilopartalj@gmail.com", "latRodectuS", email, "Password reset", getMessagePassReset(u));
                    ufr.edit(u);
                    FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Vasa sifra vam je prosledjena e-mailom!"));
                    username = "";
                    email = "";
                } catch (AddressException ex) {
                    FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_WARN, "Greska pri registraciji", "Email adresa nije u odgovarajucem formatu!"));
                } catch (MessagingException ex) {
                    FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Greska pri registraciji", "Doslo je do greske i poruka nije poslata!"));
                } catch (Exception ex) {
                    
                }
            } else{
                FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Pogresno uneti podaci. Pokusajte ponovo."));
            }
        } else{
            FacesContext.getCurrentInstance().addMessage(sessionId, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Korisnik "+username+" jos uvek nije registrovan."));

        }
    }
    
    private String getMessagePassReset(User u) throws UnknownHostException{
        return "Vasa sifra je: "+ u.getPassword()+"\n" + "Molimo vas pratite ovaj link kako biste aktivirali vas nalog http://srbdroid.no-ip.info/faces/login.xhtml?id=" + sessionId;
    }
    
    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
}
